<?php
/**
*
* @package MoltX QuickChat Loader
* @author $Author: moltendorf $
* @version $Id: molten_chat_common.php 158 2009-09-25 07:32:46Z moltendorf $
* @latest $URL: https://molten-chat.googlecode.com/svn/trunk/source/includes/molten_chat/molten_chat_common.php $
* @copyright (c) 2005-2009 Matthew Oltendorf
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

// PHP: Hypertext Preprocessor Document

/*
	Unfinished Items:
		1.
		2.
		3.
		4.
		5.
*/

/*
	Known Issues:
		1.
		2.
		3.
		4.
		5.
*/

// Style: Procedural.

molten_chat_common :: set_handlers ();

try
	{
		// This is checked in all included files to ensure that it was not executed remotely.
		define ('molten_chat', true, true);
	}
catch (Exception $exception)
	{
		// Swallow error.
	}

if (
	!defined ('molten_chat_include')
)
	{
		// Define include path.
		define ('molten_chat_include', './include/', true);
	}

if (
	!defined ('molten_chat_rooms')
)
	{
		// Define rooms path.
		define ('molten_chat_rooms', molten_chat_include . 'rooms/', true);
	}

try
	{
		// If the timezone is not properly set, it will throw an exception.
		date_default_timezone_get ();
	}
catch (Exception $exception)
	{
		// We set the timezone properly.
		date_default_timezone_set ('UTC');
	}

// Begin including files.

// Load configuration.
require (molten_chat_include . 'molten_chat_information.php');
require (molten_chat_include . 'molten_chat_configuration.php');
molten_chat_information :: initialize ();

// Load database.
require (molten_chat_include . 'database/molten_chat_database_' . molten_chat_configuration :: $database . '.php');

// Load cache core.
require (molten_chat_include . 'molten_chat_cache.php');

// Load cache.
require (molten_chat_include . 'cache/molten_chat_cache_' . molten_chat_configuration :: $cache . '.php');

// Load module.
require (molten_chat_include . 'module/molten_chat_module_' . molten_chat_configuration :: $module . '.php');

// Load server.
require (molten_chat_include . 'molten_chat_server.php');

// Include slash commands.
require (molten_chat_include . 'molten_chat_functions.php');

if (
	!isset ($load)
)
	{
		molten_chat_server :: initialize ();
	}

molten_chat_common :: restore_handlers ();

// Style: Object Oriented.

/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
// Name: molten_chat_common
// Type: Singleton.
// Title: MoltX Chat Common Object.
// Description: Mostly error handling functions.
// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
class molten_chat_common
	{
		// For error handling.
		private static $set = false;

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: __construct
		// Title: Class construction.
		// Variables: None.
		// Description: Disabled.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		private function __construct ()
			{
				// No further actions required.
				return false;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: __destruct
		// Title: Class destruction.
		// Variables: None.
		// Description: Disabled.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		private function __destruct ()
			{
				// No further actions required.
				return false;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: error_handler
		// Title: Error handler.
		// Variables: $type, $message, $file, $line, $context
		//	1.	$type: Does nothing.
		//	2.	$message: Does nothing.
		//	3.	$file: Does nothing.
		//	4.	$line: Does nothing.
		//	5.	$context: Does nothing.
		// Description: Converts errors to exceptions. Used by internal functions as the preferred error handler.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		public static function error_handler ($type, $message, $file = null, $line = null, $context = null)
			{
				// Throw an ErrorException error to exception conversion exception.
				throw new ErrorException ($message, 0, $type, $file, $line);

				// Don't execute internal PHP error handler.
				return true;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: exception_handler
		// Title: Exception handler.
		// Variables: $exception
		//	1.	$exception: The exception that was thrown.
		// Description: Handles exceptions within this object.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		public static function exception_handler ($exception)
			{
				// Output exception in a nice, fancy, easy-to-read form.
				echo	'<div style="border-stytle:dash; border-width:1px; "><div style="font-size:22px; text-align:center; "><strong>Uncaught Exception</strong></div><br /><strong>Response:</strong> ',
						htmlentities ($exception -> getMessage (), ENT_QUOTES),
						'<br /><strong>File:</strong> ',
						htmlentities ($exception -> getFile (), ENT_QUOTES),
						'<br /><strong>Line:</strong> ',
						htmlentities ($exception -> getLine (), ENT_QUOTES),
						'<br /><strong>Traceback:</strong><br /><pre>',
						htmlentities ($exception -> getTraceAsString (), ENT_QUOTES),
						'</pre></dv><br />';

				// Redundant.
				return true;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: set_handlers
		// Title: Set error handlers.
		// Variables: None.
		// Description: Set the error handlers for use within all molten chat code.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		public static function set_handlers ()
			{
				if (
					self :: $set
				)
					{
						return false;
					}

				set_error_handler (array (__CLASS__, 'error_handler'));
				set_exception_handler (array (__CLASS__, 'exception_handler'));

				self :: $set = true;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: standardize_link
		// Title: Parse a remote link.
		// Variables: $link
		//	1.	$link: The link to parse.
		// Description: Ensure that a link has a protocol in front of it.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		public static function standardize_link (&$link)
			{
				if (
					!is_array ($link)
				)
					{
						return false;
					}

				// Make sure uri is a string.
				$link ['uri'] = molten_chat_server :: to_string ($link ['uri']);

				if (
					$link ['local'] === true
				)
					{
						return true;
					}

				$protocol = preg_match ('/^(ht|f)tps?:\\/\\//i', $link ['uri']);

				// Check if http:// was added.
				if (
					empty ($protocol)
				)
					{
						$link ['uri'] = 'http://' . $link ['uri'];
					}

				return true;
			}

		/* --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- //
		// Name: restore_handlers
		// Title: Restore error handlers.
		// Variables: None.
		// Description: Restor the error handlers to the original ones.
		// --|-- ----- ----- ----- -----|----- ----- ----- ----- --|-- */
		public static function restore_handlers ()
			{
				if (
					!self :: $set
				)
					{
						return false;
					}

				restore_error_handler ();
				restore_exception_handler ();

				self :: $set = false;
			}
	}